import { request } from '../../request/index'
Page({

  data: {
    appId: "xxxxx",
    userInfo: {},
    extraData: {
      // 把1221数字换成你的产品ID，否则会跳到别的产品
      id : "379781",
      // 自定义参数，具体参考文档
      customData : {
          clientInfo: `iPhone OS 10.3.1 / 3.2.0.43 / 0`,
          imei: 'xxxxx'
      }
  }
    // userId: wx.getStorageSync('userId'),
  },


  // 监听消息事件
  onMessage(data) {
    if (getApp().globalData.wdxx > 0) {
      wx.setTabBarBadge({
        index: 3, //从左边开始第几个页面显示
        text: '' + getApp().globalData.wdxx //消息条数
      })
    } else {
      wx.removeTabBarBadge({
        index: 3
      })
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.userId = wx.getStorageSync('userId')
    wx.setNavigationBarColor({
      frontColor: '#ffffff',
      backgroundColor: '#afdfe4'
    })
    
    this.setUser()

    this.getFellow(this.userId, 0)
  },

  onShow() {
    // 设置未读消息条数
    if (getApp().globalData.wdxx > 0) {
      wx.setTabBarBadge({
        index: 3, //从左边开始第几个页面显示
        text: '' + getApp().globalData.wdxx //消息条数
      })
    } else {
      wx.removeTabBarBadge({
        index: 3
      })
    }
  },

  setUser() {
    const userInfo = wx.getStorageSync("userInfo")
    if (userInfo) {
      this.setData({
        userId: wx.getStorageSync('userId'),
        userInfo
      })
    }
  },

  getuserInfo() {
    wx.getUserProfile({
      desc: "获取用户相关信息",
        success: (res)=>{
        console.log(res)
        wx.setStorageSync("user", res.userInfo)
        this.setData({
          user:res.userInfo
        })
      },
      fail: ()=>{
        console.log("授权失败")
      },
      complete: ()=>{}
    })
  },
  personalHandle() {
    wx.navigateTo({
      url: `/pages/personal/personal?userId=${this.userId}`,
      fail: (err)=>{
        console.log("跳转失败",err)
      }
    })
  },

  handUsers(e) {
    const { type } = e.currentTarget.dataset
    let users = null
    if(type ===0 ) {
      users = this.fellows
    } else {
      users = this.fans
    }
    wx.navigateTo({
      url: `/pages/userline/userline?type=${type}&users=${JSON.stringify(users)}`
    })
  },

  // 获取关注列表
  async getFellow(userid, curPage) {
    const { data: res1 } = await request({
      url: `/follows/${userid}/${curPage}`,
      method: 'post'
    })
    const { data: res2 } = await request({
      url: `/fans/${userid}/${curPage}`,
      method: 'post'
    })
    const users1 = res1.users.users
    users1.forEach(item => {
      const headpic = item.headpic.split('"')[1]
      const nickname = item.nickname.split('"')[1]
      item.headpic = headpic
      item.nickname = nickname
    })
    const users2 = res2.users.users
    users2.forEach(item => {
      const headpic = item.headpic.split('"')[1]
      const nickname = item.nickname.split('"')[1]
      item.headpic = headpic
      item.nickname = nickname
    })
    this.setData({
      fellows: res1.users.totalCount,
      fans: res2.users.totalCount
    })
    this.fellows = users1
    this.fans = users2
  },

  handCancle() {
    wx.showModal({
      title: '提示',
      content: '该功能还在完善中哦',
      success (res) {
        if (res.confirm) {
          console.log('用户点击确定')
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  }
})